
Panels.Commodities.Options = {Planets: false,
                              Ticks: 1,
                              Selected: false};


Panels.Commodities.Display = function()
{
  Panels.Commodities.Options.Planets = $("CommoditiesIncludePlanets").checked;
  Panels.Commodities.Options.Selected = $("CommoditiesOnlySelected").checked;

  if (Panels.Commodities.Options.Ticks == 1)
  {
    $("CommoditiesTick").set("class", "navlink_on");
    $("CommoditiesDay").set("class", "navlink_off");
    $("CommoditiesWeek").set("class", "navlink_off");
  }
  else if (Panels.Commodities.Options.Ticks == 4)
  {
    $("CommoditiesTick").set("class", "navlink_off");
    $("CommoditiesDay").set("class", "navlink_on");
    $("CommoditiesWeek").set("class", "navlink_off");
  }
  else if (Panels.Commodities.Options.Ticks == 28)
  {
    $("CommoditiesTick").set("class", "navlink_off");
    $("CommoditiesDay").set("class", "navlink_off");
    $("CommoditiesWeek").set("class", "navlink_on");
  }

  var planet = Panels.Commodities.Options.Planets;
  var ticks = Panels.Commodities.Options.Ticks;
  var onlySelected = Panels.Commodities.Options.Selected;

  var html = [];
  var list = [];
  var upkeep = [];
  var production = [];
  var balance = [];
  var objects = [];

  if (onlySelected)
  {
    Utility.Tile.GetSelected(eBuilding).each(function(item) { objects.push(Utility.Object.Get(eBuilding, item.id)); });
    if(planet)
    {
      Utility.Tile.GetSelected(ePlanet).each(function(item){ objects.push(Utility.Object.Get(ePlanet, item.id)); });
      Utility.Tile.GetSelected(eStarbase).each(function(item) { objects.push(Utility.Object.Get(eStarbase, item.id)); });
    }
  }
  else
  {
    objects.extend($A(Utility.Object.GetList(eBuilding)));
    if(planet)
    {
      objects.extend($A(Utility.Object.GetList(ePlanet)));
      objects.extend($A(Utility.Object.GetList(eStarbase)));
    }
  }

  for(var i = 0; i < objects.length; i++)
  {
    var object = objects[i];
    var thisUpkeep = object.calculateUpkeep();
    var thisProduction = object.calculateProduction();
    
    for(var commodity in thisUpkeep)
    {
      if (!thisUpkeep.hasOwnProperty(commodity))
        continue;
        
      if(upkeep[commodity] == undefined)
        upkeep[commodity] = 0;
        
      upkeep[commodity] += thisUpkeep[commodity] * ticks;
    }
    
    for(var commodity in thisProduction)
    {
      if (!thisProduction.hasOwnProperty(commodity))
        continue;
        
      if(production[commodity] == undefined)
        production[commodity] = 0;
        
      production[commodity] += thisProduction[commodity] * ticks;
    }
  }

  var html = [];

  for(var commodity in Global.Commodities)
  {
    if(!Global.Commodities.hasOwnProperty(commodity) && upkeep[commodity] == undefined && production[commodity] == undefined)
      continue;
      
    if(upkeep[commodity] == undefined)
      upkeep[commodity] = 0;
      
    if(production[commodity] == undefined)
      production[commodity] = 0;
      
    balance[commodity] = production[commodity] - upkeep[commodity];

    html.push("<tr class='infoTableColor" + (html.length % 2 == 1 ? "1" : "2") + "'>"
            + "<td><img src='" + Utility.Config.Get("Imagepack") + "/res/" + Global.Commodities[commodity].image + "'></td>"
            + "<td>" + commodity + "</td>"
            + "<td style='color: " + (production[commodity] != 0? "green": "") + ";'>" + production[commodity].addCommas() + "</td>"
            + "<td style='color: " + (upkeep[commodity] != 0 ? "red" : "") + ";'>" + upkeep[commodity].addCommas() + "</td>"
            + "<td style='color: " + (balance[commodity] > 0? "green": (balance[commodity] < 0? "red": (upkeep[commodity] != 0 && production[commodity] != 0? "yellow": ""))) + ";'>" + balance[commodity].addCommas() + "</td>"
            + "</tr>");
  }

  var w = "<table class='commodityTable'>";
  
  if(html.length > 0)
  {

    w += "<tr class='commodityHeading'>"
       + "<th colspan='2' align='center'>Commodity</th>"
       + "<th align='center'>Produced</th>"
       + "<th align='center'>Consumed</th>"
       + "<th align='center'>Balance</th></tr>";
    w += html.join("");
  }
  
  else
  {
    w += "<tr><td colspan='5' align='center'>No Data to Display</td></tr>";
  }
  
  w += "</table>";
  
  $("CommodityTableArea").set("html", w);
}

MainTabControl.addEvent("active", function(id, section, tab)
{
  if (section.get("id") == "CommoditiesTab")
    Panels.Commodities.Display();
});